package test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.event.KeyEvent;

public class PressedKey extends JPanel implements KeyListener {
	JLabel pic;

	public PressedKey() {
		super(new GridLayout(1, 1));
		JPanel panel1 = new JPanel(new GridLayout(1, 1));
		pic = new JLabel("", JLabel.CENTER);
		panel1.add(pic);
		panel1.setFocusable(true);
		pic.setFocusable(true);
		pic.requestFocusInWindow();
		panel1.addKeyListener(this);
		add(panel1);
	}

	public void keyPressed(KeyEvent e) {
		/*
		 * int keyCode = e.getKeyCode();
		 * 
		 * String key=KeyEvent.getKeyText(keyCode);
		 * 
		 * 
		 * 
		 * System.out.println(key); if(key=="A"||key=="a") {
		 * 
		 * 
		 * try { File f1=new File("C:/a.jpg"); Image image = ImageIO.read(f1);
		 * ImageIcon icon=new ImageIcon(image); pic.setIcon(icon); }
		 * catch(Exception ex){}
		 * 
		 * }
		 */
	}

	public void keyTyped(KeyEvent e) {

		this.disp(e);
	}

	public void keyReleased(KeyEvent e) {

	}

	public void disp(KeyEvent e) {

		System.out.println("hi");
		char key = e.getKeyChar();
		// String s=""+key+"";
		if (key >= 65 && key <= 90 || key >= 97 && key <= 122) {

			char upper = Character.toUpperCase(key);
			char lower = Character.toLowerCase(key);
			String s = "" + upper + "" + lower + "";
			/*
			 * try { File f1=new File("C:/a.jpg"); Image image =
			 * ImageIO.read(f1); ImageIcon icon=new ImageIcon(image);
			 * pic.setIcon(icon); } catch(Exception ex){}
			 */
			java.awt.Font font = new java.awt.Font("Comic Sans", Font.BOLD, 70);
			pic.setFont(font);
			pic.setText(s);
		} else if (!(key >= 65 && key <= 90 || key >= 97 && key <= 122)) {
			java.awt.Font font = new java.awt.Font("Comic Sans", Font.BOLD, 70);
			pic.setFont(font);
			pic.setText("OOPS!!");
		} else {
			System.exit(0);
		}
	}

	public static void main(String args[]) throws IOException {
		JFrame frame = new JFrame("Alphabets");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		PressedKey p = new PressedKey();
		frame.add(p);
		frame.pack();
		frame.setBounds(150, 100, 800, 580);
		frame.setVisible(true);
	}
}
